#include "gvj.h"
#include "proxy_process.h"
#include "proxy.h"
#include "handler_poll.h"
#include "hdu.h"
#include "hdu_handler.h"
#include "hdu_deserialize.h"

#include <cstdio>
#include <cstring>
#include <unistd.h>

typedef Proxy<Hdu, HduDeserialize, HduHandler, HduTask> HduProxy;

// 代理进程的入口
//int hdu_proxy_process(int *listen_to_proxy, int *proxy_to_listen, int msg_id, const char *cookie_dir) {
int hdu_proxy_process(int in_pipe, int out_pipe, int msg_id, const char *cookie_dir) {
	// 测试监听进程和代理进程之间的管道是否建立成功
	/*
	pid_t pid = getpid();
	char buff[100];
	ssize_t n;

	memset(buff, 0, sizeof(buff));
	sprintf(buff, "hello i am poj proxy: %d", pid);
	n = write(out_pipe, buff, strlen(buff));
	printf("poj write: %d\n", n);

	memset(buff, 0, sizeof(buff));
	n = read(in_pipe, buff, 100);
	printf("poj read: %d\n", n);
	printf("poj proxy(%d): %s\n", pid, buff);
	
	while (true) {}
	exit(0);
	*/
	// 测试监听进程和代理进程之间的管道是否建立成功
	
	
	//int in_pipe, out_pipe, res;
	//set_pipe(listen_to_proxy, proxy_to_listen, &in_pipe, &out_pipe);
	set_process_name("gvj_hdu_proxy");
	
	int res = init_python();
	if (!res) {
		printf("init python error\n");
		exit(0);
	}

	HduProxy *hdu_proxy = new HduProxy(in_pipe, out_pipe, msg_id, cookie_dir);
	res = hdu_proxy->main_process();

	finalize_python();
	exit(res);
	
}
